<?php namespace App\Admin\Supplier\Supplier;

//基础条件
use App\Core\Repository\Criterias\FixedCriteria;
use App\Core\Repository\Criterias\PaginateCriteria;
use App\Core\Repository\Criterias\SortCriteria;
//过滤器
use App\Core\Repository\Criterias\Filters\InputCriteria;
use App\Core\Repository\Criterias\Filters\InputKeywordsCriteria;
use App\Core\Repository\Criterias\Filters\InputTimeRangeCriteria;

use Input;
use App\Admin\Access\User\Repository as UserRepository;


trait Service {
    
    //基础服务
    use \App\Core\Service\Service;
    use \App\Core\Service\Services\All;
    use \App\Core\Service\Services\Indexer;
    use \App\Core\Service\Services\Creater;
    use \App\Core\Service\Services\Updater;
    use \App\Core\Service\Services\Changer;
    use \App\Core\Service\Services\Deleter;
    use \App\Core\Service\Services\Detailer;
    //操作日志
    use \App\Admin\Base\LogService;
    
    protected function bootService(){
        $this->bootBaseService();
        $this->setRepository(new Repository);
        $this->setMeta(new Meta);
        $this->repository = $this->getRepository();
        //添加观察者
        $this->addListeners([
            'create' => new Listeners\CreateListener()
        ]);
    }
	
    //列表
    public function lists(){
        //过滤器
        $conditions = [
            new InputKeywordsCriteria('name'),
            new InputTimeRangeCriteria('created_at'),
            new InputCriteria('status')
        ];
        $count = $this->repository->addCriterias($conditions)->count();
        $conditions[] = new PaginateCriteria();
        $conditions[] = new SortCriteria('id', 'DESC');
        $data = $this->repository->resetQuery()->resetCriteria()->addCriterias($conditions)->all();
        $data->load('admin');
        return $this->success([
            'count' => $count,
            'data' => $data
        ]);
    }
    
    //开启
    public function open(){
        return $this->change('status', '1', '0');
    }
    
    //禁用
    public function close(){
        return $this->change('status', '0', '1');
    }

    public function resetPassword(){
        $id = Input::get('id', '');
        $passwordInput = Input::get('password', '');
        if(!is_numeric($id) || !$passwordInput){
            return $this->inputError('参数有误');
        }
        $supplier = $this->repository->addCriteria(new FixedCriteria('id', $id))->first();
        if(!$supplier){
            return $this->inputError('供应商不存在');
        }
        if(!$supplier->admin){
            return $this->inputError('账号不存在');
        }
        try {
            $saltcode = $saltcode = rand(10000, 99999);
            $password = md5($passwordInput.$saltcode);
            with(new UserRepository())->addCriteria(new FixedCriteria('user_id', $supplier->admin->user_id))->update([
                'saltcode' => $saltcode,
                'password' => $password,
                'login_error_count' => '0',
                'islocked' => '0'
            ]);
            return $this->success();
        } catch (\Exception $ex) {
            return $this->sysError($ex);
        }
    }

}